package handler

import (
	"cargo-hub/cache"
	"cargo-hub/model"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CargoAdd(c *gin.Context) {
	carCode := c.PostForm("car_code")
	carName := c.PostForm("car_name")
	carNo := c.PostForm("car_no")
	if carCode == "" || carName == "" || carNo == "" {
		c.JSON(20, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	cargo := model.Cargo{
		CarCode: carCode,
		CarName: carName,
		CarNo:   carNo,
	}
	err := model.DB.Create(&cargo).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("order_code")
	orderFrom := c.PostForm("order_from")
	orderTo := c.PostForm("order_to")
	orderGood := c.PostForm("order_good")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	if orderCode == "" || orderFrom == "" || orderTo == "" || orderGood == "" || cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	order := model.Order{
		OrderCode: orderCode,
		OrderFrom: orderFrom,
		OrderTo:   orderTo,
		OrderGood: orderGood,
		Cid:       cid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  orders.id as OrderId,
		  cargos.car_code as CarCode,
		  cargos.car_name as CarName,
		  cargos.car_no as CarNo,
		  orders.order_code as OrderCode,
		  orders.order_from as OrderFrom,
		  orders.order_to as OrderTo,
		  orders.order_good as OrderGood
		FROM
		  orders
		LEFT JOIN cargos ON orders.cid = cargos.id;
	*/
	var list []model.CargoOrderList
	redis_key := "cargo_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("orders").
			Select("orders.id as OrderId",
				"cargos.car_code as CarCode",
				"cargos.car_name as CarName",
				"cargos.car_no as CarNo",
				"orders.order_code as OrderCode",
				"orders.order_from as OrderFrom",
				"orders.order_to as OrderTo",
				"orders.order_good as OrderGood",
			).
			Joins("LEFT JOIN cargos ON orders.cid = cargos.id").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.CargoOrderList
	model.DB.Table("orders").
		Select("orders.id as OrderId",
			"cargos.car_code as CarCode",
			"cargos.car_name as CarName",
			"cargos.car_no as CarNo",
			"orders.order_code as OrderCode",
			"orders.order_from as OrderFrom",
			"orders.order_to as OrderTo",
			"orders.order_good as OrderGood",
		).
		Joins("LEFT JOIN cargos ON orders.cid = cargos.id").
		Where("orders.id = ?", id).
		Debug().
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
